﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.IManager;
using Business.Manager;
using Entity;
using System.Data;

namespace OnlinePayment.Admin
{
    public partial class AnnouncementEdit : System.Web.UI.Page
    {
        public static int id;
        AnnouncementManager announcementMannager = new AnnouncementManager();
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                id = Convert.ToInt32(Request.QueryString["id"].ToString());
            }
            catch
            {
                Response.Write("<script language='javascript'>alert('参数错误！');</script>");
            }
            if (!Page.IsPostBack)
            {
                
                if (Session["username"] != null)
                {
                    Response.Write("<script language='javascript'>alert('非法操作，请先登录！');location.href = 'admin_login.aspx';</script>");
                }
                else
                {
                    articlebind();
                }
            }
        }

        private void articlebind()
        {
            Announcement announcement = announcementMannager.getAnnouncement(id);
            txtAuthor.Text=announcement.Athour ;
            txtTitle.Text=announcement.Title;
            txtContent.Text = Server.HtmlDecode(announcement.Content);
            txtKeyword.Text=announcement.KeyWord ;
        }
        protected void btn_savearticle_Click(object sender, EventArgs e)
        {
            Announcement announcement = new Announcement();
            announcement.Athour = txtAuthor.Text;
            announcement.Title = txtTitle.Text;
            announcement.Content = Server.HtmlEncode(txtContent.Text);
            announcement.KeyWord=txtKeyword.Text;
           /* String s = txtTitle.Text;
            String s1 = txtAuthor.Text;
            String s2 = txtContent.Text;
            String s3 = txtKeyword.Text;*/


            if (announcementMannager.updateAnnouncement(announcement, id))
            {

                Response.Write("<script language=javascript>alert('文章修改成功!');location='AnnoucementList.aspx'</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('文章修改失败！');location='javascript:history.go(-1)'</script>");
            } 
        }

        protected void Page_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            if (ex is HttpRequestValidationException)
            {
                Response.Write("<script language=javascript>alert('字符串含有非法字符！')</script>");
                Server.ClearError(); // 如果不ClearError()这个异常会继续传到Application_Error()
                Response.Write("<script language=javascript>window.location.href='AnnouncementEdit.aspx';</script>");
            }
        }

        protected void btn_back_Click(object sender, EventArgs e)
        {

        }

        protected void btn_prearticle_Click(object sender, EventArgs e)
        {
            Announcement announcement = new Announcement();
            announcement.Athour = txtAuthor.Text;
            announcement.Title = txtTitle.Text;
            announcement.Content = txtContent.Text;
            announcement.KeyWord = txtKeyword.Text;
            Session["PreviewAnnouncement"] = announcement;
            Response.Write("<script>window.open('PreviewAnnoucement.aspx','_blank')</script>");
           

        }
    }
}